package com.example.demo.leetcode.classics150;

import java.util.LinkedList;
import java.util.List;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _102组合
 * @description
 * @date 2023/08/10 14:23
 * <p>
 * ******************************************************
 */
public class _102组合 {

    List<List<Integer>> res = new LinkedList<>();
    LinkedList<Integer> track = new LinkedList<>();

    public List<List<Integer>> combine(int n, int k) {
        if (n < 1 || k > n) {
            return res;
        }
        backtrack(1, n, k);

        return res;
    }

    private void backtrack(int start, int end, int k) {
        if (track.size() == k) {
            res.add(new LinkedList<>(track));
            return;
        }
        if (end < start) {
            return;
        }

        for (int i = start; i <= end; i++) {
            track.add(i);
            backtrack(i + 1, end, k);
            track.removeLast();
        }
    }


    public static void main(String[] args) {
        _102组合 s = new _102组合();
        System.out.println(s.combine(4, 2));
    }
}
